package cn.stylefeng.guns.sys.modular.interfaceapproval.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.interfaceapproval.entity.Interfaceapproval;
import cn.stylefeng.guns.sys.modular.interfaceapproval.mapper.InterfaceapprovalMapper;
import cn.stylefeng.guns.sys.modular.interfaceapproval.model.params.InterfaceapprovalParam;
import cn.stylefeng.guns.sys.modular.interfaceapproval.model.result.InterfaceapprovalResult;
import cn.stylefeng.guns.sys.modular.interfaceapproval.service.InterfaceapprovalService;
import cn.stylefeng.guns.sys.modular.notice.mapper.DbNoticeMapper;
import cn.stylefeng.guns.sys.modular.notice.model.params.DbNoticeParam;
import cn.stylefeng.guns.sys.modular.notice.model.result.DbNoticeResult;
import cn.stylefeng.guns.sys.modular.notice.service.DbNoticeService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 接口审批列表 服务实现类
 * </p>
 *
 * @author author
 * @since 2020-08-31
 */
@Service
public class InterfaceapprovalServiceImpl extends ServiceImpl<InterfaceapprovalMapper, Interfaceapproval> implements InterfaceapprovalService {

    @Autowired
    private InterfaceapprovalMapper interfaceapprovalMapper;
    @Autowired
    private DbNoticeService dbNoticeService;

    @Override
    public void add(InterfaceapprovalParam param) {
        Interfaceapproval entity = getEntity(param);


        LoginUser user = LoginContextHolder.getContext().getUser();

        //保存一条通知
        DbNoticeParam dbNoticeParam = new DbNoticeParam();
        DbNoticeResult result = null;
        if (param.getContent().equals("1")) {
            dbNoticeParam.setSpState("0");
            dbNoticeParam.setState("0");
            dbNoticeParam.setAdminState("0");
            dbNoticeParam.setContent("数据对比申请");
            dbNoticeParam.setXxContent("新增 " + param.getSystem() + " 应用系统数据对比");
            dbNoticeParam.setInterfaceApprovalId(entity.getId());
            dbNoticeParam.setDepartment(user.getDeptName());
            dbNoticeParam.setServiceType("1");
            dbNoticeParam.setUserId(String.valueOf(user.getId()));
            dbNoticeParam.setRemark(param.getDescription());
            dbNoticeParam.setSystem(param.getSystem());
            dbNoticeService.add(dbNoticeParam);
            result = dbNoticeService.findByParam(dbNoticeParam);
        }
        if (param.getContent().equals("2")) {
            dbNoticeParam.setSpState("0");
            dbNoticeParam.setState("0");
            dbNoticeParam.setAdminState("0");
            dbNoticeParam.setContent("接口定制申请");
            dbNoticeParam.setXxContent("新增 " + param.getSystem() + " 应用系统接口定制");
            dbNoticeParam.setInterfaceApprovalId(entity.getId());
            dbNoticeParam.setDepartment(user.getDeptName());
            dbNoticeParam.setServiceType("1");
            dbNoticeParam.setUserId(String.valueOf(user.getId()));
            dbNoticeParam.setRemark(param.getDescription());
            dbNoticeParam.setSystem(param.getSystem());
            dbNoticeService.add(dbNoticeParam);
            result = dbNoticeService.findByParam(dbNoticeParam);
        }

        entity.setNoticeId(result.getId());
        this.save(entity);
    }

    @Override
    public void delete(InterfaceapprovalParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(InterfaceapprovalParam param) {
        Interfaceapproval oldEntity = getOldEntity(param);
        Interfaceapproval newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public InterfaceapprovalResult findBySpec(InterfaceapprovalParam param) {
        return null;
    }

    @Override
    public List<InterfaceapprovalResult> findListBySpec(InterfaceapprovalParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(InterfaceapprovalParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public Integer updateApprove(Integer param) {
        //批准后修改通知中心内容
        //获取通知id
        InterfaceapprovalParam interfaceapprovalParam = new InterfaceapprovalParam();
        interfaceapprovalParam.setId(param);
        List<InterfaceapprovalResult> list = this.baseMapper.customList(interfaceapprovalParam);
        //修改通知中心内容
        DbNoticeParam dbNoticeParam = new DbNoticeParam();
        if (list.get(0).getContent().equals("1")) {
            dbNoticeParam.setAdminState("0");
            dbNoticeParam.setState("0");
            dbNoticeParam.setSpState("1");
            dbNoticeParam.setId(list.get(list.size() - 1).getNoticeId());
        }
        if (list.get(0).getContent().equals("2")) {
            dbNoticeParam.setAdminState("0");
            dbNoticeParam.setState("0");
            dbNoticeParam.setSpState("2");
            dbNoticeParam.setId(list.get(list.size() - 1).getNoticeId());
        }
        dbNoticeService.update(dbNoticeParam);

        return interfaceapprovalMapper.Approve(param);
    }

    @Override
    public Integer updateReject(Integer param) {
        //驳回后修改通知中心内容
        //获取通知id
        InterfaceapprovalParam interfaceapprovalParam = new InterfaceapprovalParam();
        interfaceapprovalParam.setId(param);
        List<InterfaceapprovalResult> list = this.baseMapper.customList(interfaceapprovalParam);
        //修改通知中心内容
        DbNoticeParam dbNoticeParam = new DbNoticeParam();
        dbNoticeParam.setAdminState("0");
        dbNoticeParam.setState("0");
        dbNoticeParam.setSpState("3");
        dbNoticeParam.setId(list.get(list.size() - 1).getNoticeId());

        dbNoticeService.update(dbNoticeParam);

        return interfaceapprovalMapper.Reject(param);
    }

    private Serializable getKey(InterfaceapprovalParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Interfaceapproval getOldEntity(InterfaceapprovalParam param) {
        return this.getById(getKey(param));
    }

    private Interfaceapproval getEntity(InterfaceapprovalParam param) {
        Interfaceapproval entity = new Interfaceapproval();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
